﻿/*
版权所有：版权所有(C) 2010，刘建新
文件编号：M01_NewsBll.cs
文件名称：NewsBll.cs
系统编号：Z0003007
系统名称：通用办公系统
模块编号：M01
模块名称：信息发布
设计文档：IOA_M01信息发布XDE建模.mdx
完成日期：2010/12/17
作　　者：
内容摘要：表[Market]业务操作类
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Eis51.CampanyCMS.Model.NewsManager;
using Eis51.CampanyCMS.Model;
using System.Data;
using System.Collections;
using Eis51.CampanyCMS.Model.NewsManager.QueryO;
using Eis51.CampanyCMS.Model.Bus;
using Eis51.CampanyCMS.FrameWork;
using MoreUser.Bussiness;
using Eis51.CampanyCMS.Access.NewsManager;

namespace Eis51.CampanyCMS.Business.NewsManager
{
    /// <summary>
    /// Market的BLL 自定义方法
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public partial class MarketBLL : BaseBLL<Market>
    {
        public bool Save(Market news)
        {
            if (news.Seqno == 0)
            {

                this.Insert(news);
                if (string.IsNullOrEmpty(news.Code))
                {
                    news.Code = news.Seqno.ToString();
                    this.Update(news);
                }
            }
            else
            {
                this.Update(news);
            }
            return true;
        }




        public bool SubMit(Market news)
        {

            news.Lasteupdateby = AppContext.CurrentAdministrator.UserName;
            // news.Lasteupdatedate = DateTime.Now;
            if (news.Seqno == 0)
            {
                news.Createby = AppContext.CurrentAdministrator.UserName;
                // news.Createdate = DateTime.Now;
                this.Insert(news);
                if (string.IsNullOrEmpty(news.Code))
                {
                    news.Code = news.Seqno.ToString();
                    this.Update(news);
                }
            }
            else
            {
                this.Update(news);
            }

            BuildPageBLL.CreateHtml(RunType.Market, news);
            return true;
        }

        public static Market GetnewsByType(string Username, int type, int lcid)
        {
            string sql = "select * from Market where Createby='{0}' and Type={1} and  Licd={2} ";
            sql = string.Format(sql, Username, type, lcid);
            DataTable dt = MarketBLL.GetInstance().GetDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                return Market.Dr2Obj(dt.Rows[0], BaseModel.ColumnNameEnum.DBName);
            }
            return null;
        }

        public IList Search(MarketQuery query)
        {
            string sql = "select * from Market where Licd={0} ";
            sql = string.Format(sql, (int)query.Licd);
            if (query.Title != "")
            {
                sql += " and  Title like '%{0}%' ";
                sql = string.Format(sql, query.Title.Replace("'", "''"));
            }
            if (query.PublishBeginDate != "")
            {
                sql += " and  CreateDate >= #{0} 00:00:00# ";
                sql = string.Format(sql, query.PublishBeginDate);
            }
            if (query.PublishEndDate != "")
            {
                sql += " and  CreateDate <= #{0} 23:59:59# ";
                sql = string.Format(sql, query.PublishEndDate);
            }
            if (query.Type > 0)
            {
                sql += " and  Type={0} ";
                sql = string.Format(sql, query.Type);
            }
            if (query.IsFist)
            {
                sql += " and  IsFist>0 ";
                sql = string.Format(sql, query.Type);
            }

            //sql += " and  Createby ='{0}' ";
            //sql = string.Format(sql, query.CreateBy);


            sql += " order by show_idx desc,Lasteupdateby desc ";
            DataTable dt = this.GetDataTable(sql);
            return Market.Dt2Objs(dt, Market.ColumnNameEnum.DBName);
        }

        public DataTable Search(MarketQuery query, int first, int pagesize)
        {
            string sql = "select *,year(createdate) &'-'& month(createdate)&'-'& day(createdate) as NEWDATE from Market where Licd={0} ";
            sql = string.Format(sql, (int)query.Licd);
            if (query.Title != "")
            {
                sql += " and  Title like '%{0}%' ";
                sql = string.Format(sql, query.Title.Replace("'", "''"));
            }

            if (query.Type > 0)
            {
                sql += " and  Type={0} ";
                sql = string.Format(sql, query.Type);
            }
            else
            {
                sql += " and ( Type in (  select seqno from newstype where  Createby ='{0}' and (code ='zsk' or parentid in (select seqno from newstype where  Createby ='{0}' and code ='zsk')))) ";
            }
            sql += " and  Createby ='{0}' ";
            sql = string.Format(sql, query.CreateBy);
            sql += " order by show_idx desc,Lasteupdateby desc ";
            return BaseSearchConditonBLL.GetDataForPage(sql, first, pagesize);
        }

        public int SearchCount(MarketQuery query)
        {
            string sql = "select count(*) as count1  from Market where Licd={0} ";
            sql = string.Format(sql, (int)query.Licd);
            if (query.Title != "")
            {
                sql += " and  Title like '%{0}%' ";
                sql = string.Format(sql, query.Title.Replace("'", "''"));
            }

            if (query.Type > 0)
            {
                sql += " and  Type={0} ";
                sql = string.Format(sql, query.Type);
            }
            else
            {
                sql += " and ( Type in (  select seqno from newstype where  Createby ='{0}' and (code ='zsk' or parentid in (select seqno from newstype where  Createby ='{0}' and code ='zsk')))) ";
            }
            sql += " and  Createby ='{0}' ";
            sql = string.Format(sql, query.CreateBy);
            DataTable dt = this.GetDataTable(sql);
            return int.Parse(dt.Rows[0]["count1"].ToString());

        }

    }


    #region 代码自动生成
    /// <summary>
    /// Market的BLL
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public partial class MarketBLL : BaseBLL<Market>
    {
        #region 自动生成代码：取得相关实体

        /// <summary>
        /// 方法名称: MarketBLL
        /// 内容摘要: 构造函数进行初始化
        /// </summary>
        protected MarketBLL()
        {
        }
        private static volatile MarketBLL m_instance = null;
        /// <summary>
        /// 方法名称: GetInstance
        /// 内容摘要: 取得 MarketBLL 对象
        /// </summary>
        /// <returns>返回一个已经存在的实体</returns>
        public static MarketBLL GetInstance()
        {
            // 通用的必要代码 iBatisNet双校检机制,如果实例不存在
            if (m_instance == null)
            {
                lock (typeof(MarketBLL))
                {

                    // 如果实例不存在
                    if (m_instance == null)
                    {

                        // 创建一个的实例
                        m_instance = new MarketBLL();
                    }
                }
            }

            // 返回业务逻辑对象
            return m_instance;
        }

        #endregion

        #region 自动生成代码：基本业务公开函数

        /// <summary>
        /// 方法名称: Get
        /// 内容摘要: 由键值获取一个实体对象
        /// </summary>
        /// <returns>Market</returns>
        public Market Get(Market.Key key)
        {

            return MarketDao.GetInstance().SelectByID(key);
        }
        /// <summary>
        /// 方法名称: Select
        /// 内容摘要: 基本查询，不带任何条件的查询
        /// </summary>
        public IList Select()
        {
            return MarketDao.GetInstance().SelectAll();
        }
        /// <summary>
        /// 方法名称: Insert
        /// 内容摘要: 插入一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Insert(Market obj)
        {
            int result = 0;
            try
            {
                result = MarketDao.GetInstance().Insert(obj);
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

        /// <summary>
        /// 方法名称: Update
        /// 内容摘要: 更新一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Update(Market obj)
        {
            MarketDao.GetInstance().Update(obj);
            return 1;
        }

        /// <summary>
        /// 方法名称: Delete
        /// 内容摘要: 删除一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Delete(Market obj)
        {
            bool result = MarketDao.GetInstance().DeleteByID(new Market.Key(obj.Seqno));
            return result ? 1 : 0;
        }

        #endregion

    }
    #endregion
}